package com.zjdiepu.www.biz.base;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.base.DpBaseDealerInfoSubMapper;
import com.zjdiepu.www.ibiz.base.IDpBaseDealerInfoSubService;
import com.zjdiepu.www.model.base.dto.DpBaseDealerInfoSub;
import com.zjdiepu.www.model.base.ext.DpBaseDealerInfoSubExt;

/**
 * 经销商子表Service层操作实现<br/>
 * @author zhangpp<br/>
 * @time 2017年08月04日 17时27分<br/>
 * @since 1.0<br/>
 */
@Service
public class DpBaseDealerInfoSubServiceImpl extends BaseService implements IDpBaseDealerInfoSubService {
	public static final Logger logger = LoggerFactory.getLogger(DpBaseDealerInfoSubServiceImpl.class);

	@Autowired
	private DpBaseDealerInfoSubMapper dpBaseDealerInfoSubMapper;
	
	@Override
	public void saveUpdateAndDelete(DpBaseDealerInfoSub dpBaseDealerInfoSub, OperateType operType) {
		switch (operType) {
		case add:
			dpBaseDealerInfoSub.initInsertData(dpBaseDealerInfoSub.getModifier(), dpBaseDealerInfoSub.getModifierName());
			dpBaseDealerInfoSubMapper.insert(dpBaseDealerInfoSub);
			break;
		case edit:
			dpBaseDealerInfoSub.initUpdateData(dpBaseDealerInfoSub.getModifier(), dpBaseDealerInfoSub.getModifierName());
			// 影响的记录数
			int updateCount = dpBaseDealerInfoSubMapper.updateByPrimaryKeySelective(dpBaseDealerInfoSub);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			dpBaseDealerInfoSub.initDeleteData(dpBaseDealerInfoSub.getModifier(), dpBaseDealerInfoSub.getModifierName());
			// 影响的记录数
			int deleteCount = dpBaseDealerInfoSubMapper.updateByPrimaryKeySelective(dpBaseDealerInfoSub);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<DpBaseDealerInfoSub> selectByExt(DpBaseDealerInfoSubExt dpBaseDealerInfoSubExt) {
		return dpBaseDealerInfoSubMapper.selectByExt(dpBaseDealerInfoSubExt);
	}
	
	@Override
	public int countByExt(DpBaseDealerInfoSubExt dpBaseDealerInfoSubExt){
		return dpBaseDealerInfoSubMapper.countByExt(dpBaseDealerInfoSubExt);
	}
	
	@Override
	public DpBaseDealerInfoSub selectById(String id) {
		return dpBaseDealerInfoSubMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			dpBaseDealerInfoSubMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(DpBaseDealerInfoSub dpBaseDealerInfoSub) {
		return dpBaseDealerInfoSubMapper.countRefined(dpBaseDealerInfoSub);
	}

	@Override
	public DpBaseDealerInfoSub selectRefined(DpBaseDealerInfoSub dpBaseDealerInfoSub) {
		return dpBaseDealerInfoSubMapper.selectRefined(dpBaseDealerInfoSub);
	}
	
	@Override
	public int deletedRefined(DpBaseDealerInfoSub dpBaseDealerInfoSub) {
		return dpBaseDealerInfoSubMapper.deletedRefined(dpBaseDealerInfoSub);
	}

	@Override
	public boolean repeat(DpBaseDealerInfoSub dpBaseDealerInfoSub) {
		int count = countRefined(dpBaseDealerInfoSub);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(dpBaseDealerInfoSub.getId())) {
			return true;
		}
		DpBaseDealerInfoSub dbItem = selectRefined(dpBaseDealerInfoSub);
		if(null == dbItem) {
			return false;
		}
		return !dpBaseDealerInfoSub.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return dpBaseDealerInfoSubMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<DpBaseDealerInfoSub> dpBaseDealerInfoSubs) {
		return dpBaseDealerInfoSubMapper.insertBatch(dpBaseDealerInfoSubs);
	}
}
